<?php
// +----------------------------------------------------------------------
// | saiadmin [ saiadmin快速开发框架 ]
// +----------------------------------------------------------------------
// | Author: your name
// +----------------------------------------------------------------------
namespace app\cms\controller;

use plugin\saiadmin\basic\BaseController;
use app\cms\logic\SpeakerLogic;
use app\cms\model\Category;
use app\cms\model\Speaker;
use app\cms\validate\SpeakerValidate;
use support\Request;
use support\Response;

/**
 * 外籍讲者库控制器
 */
class SpeakerController extends BaseController
{
    /**
     * 构造函数
     */
    public function __construct()
    {
        $this->logic = new SpeakerLogic();
        $this->validate = new SpeakerValidate;
        parent::__construct();
    }

    /**
     * 数据列表
     * @param Request $request
     * @return Response
     */
    public function index(Request $request): Response
    {
        $where = $request->more([
            ['name', ''],
            // ['name_en', ''],
            // ['title', ''],
            ['status', ''],
            ['sort', '']
        ]);


        $query = $this->logic->search($where);
        if (!empty($where['name'])) {
            $query->whereOr('name', 'like', '%' . $where['name'] . '%')
                ->whereOr('title', 'like', '%' . $where['name'] . '%')
                ->whereOr('email', 'like', '%' . $where['name'] . '%')
                ->whereOr('tags', 'like', '%' . $where['name'] . '%')
                ->whereOr('tags_en', 'like', '%' . $where['name'] . '%')
                ->whereOr('remarks', 'like', '%' . $where['name'] . '%')
                ->whereOr('remarks_en', 'like', '%' . $where['name'] . '%')
                ->whereOr('country_cn', 'like', '%' . $where['name'] . '%')
                ->whereOr('city_cn', 'like', '%' . $where['name'] . '%')
                ->whereOr('country_en', 'like', '%' . $where['name'] . '%')
                ->whereOr('city_en', 'like', '%' . $where['name'] . '%')
                ->whereOr('time_zone', 'like', '%' . $where['name'] . '%')
                ->whereOr('new_time_zone', 'like', '%' . $where['name'] . '%')
                ->whereOr('jet_lag', 'like', '%' . $where['name'] . '%')
                ->whereOr('institution', 'like', '%' . $where['name'] . '%')
                ->whereOr('institution_cn', 'like', '%' . $where['name'] . '%')
                ->whereOr('institution_en', 'like', '%' . $where['name'] . '%')
                ->whereOr('areas_of_expertise', 'like', '%' . $where['name'] . '%')
                ->whereOr('areas_of_expertise_cn', 'like', '%' . $where['name'] . '%')
                ->whereOr('areas_of_expertise_en', 'like', '%' . $where['name'] . '%')
                ->whereOr('medical_category', 'like', '%' . $where['name'] . '%')
                ->whereOr('medical_category_cn', 'like', '%' . $where['name'] . '%')
                ->whereOr('medical_category_en', 'like', '%' . $where['name'] . '%')
                ->whereOr('presenting_meeting', 'like', '%' . $where['name'] . '%')
                ->whereOr('presenting_meeting_cn', 'like', '%' . $where['name'] . '%')
                ->whereOr('presenting_meeting_en', 'like', '%' . $where['name'] . '%')
                ->whereOr('research_sponsor', 'like', '%' . $where['name'] . '%')
                ->whereOr('association', 'like', '%' . $where['name'] . '%')
                ->whereOr('association_cn', 'like', '%' . $where['name'] . '%')
                ->whereOr('association_en', 'like', '%' . $where['name'] . '%')
                ->whereOr('journal_position', 'like', '%' . $where['name'] . '%')
                ->whereOr('journal_position_cn', 'like', '%' . $where['name'] . '%')
                ->whereOr('journal_position_en', 'like', '%' . $where['name'] . '%');
        }
        if (!empty($where['sort'])) {
            $query->order('id', $where['sort']);
        } else {
            $query->order('id', 'desc');
        }
        $data = $this->logic->getList($query);
        return $this->success($data);
    }

    /**
     * 获取讲者详情
     *
     * @Author hechengwei
     * @email chichiooo@sina.com
     * @DateTime 2024-08-20
     * @param Request $request
     * @return void
     */
    public function getDetail(Request $request)
    {
        $id = $request->input('id', '');

        $result = $this->logic->with(['passport' => function ($query) {
            $query->field('id,speaker_id,surname,name,birth,passport_number,nationality,sex,place_of_birth,passport_issuing_place,validity,upload_passport,remittance,passenger_number')->where('status', 1);
        }, 'resume' => function ($query) {
            $query->field('id,
            speaker_id,
            speaker_resume,
            speaker_resume_en,
            speaker_ppt,
            speaker_ppt_en,
            speaker_passport,
            remittance,
            passenger_number')->where('status', 1);
        }])->where('id', $id)->find();


        if ($result) {
            return $this->success($result, '操作成功');
        } else {
            return $this->fail('操作失败');
        }
    }

    /**
     * 修改邀约人数
     *
     * @Author hechengwei
     * @email chichiooo@sina.com
     * @DateTime 2024-08-20
     * @param Request $request
     * @return void
     */
    public function numberInvitations(Request $request)
    {
        $id = $request->input('id', '');
        $number_of_invitations = $request->input('number_of_invitations', '');

        $project = $this->logic->where('id', $id)->update([
            'number_of_invitations' => $number_of_invitations,
        ]);
        if ($project) {
            return $this->success('操作成功');
        } else {
            return $this->fail('操作失败');
        }
    }

    // @todo  创建讲者时生成对应中、英文 ppt

    /**
     * 获取国家
     *
     * @Author hechengwei
     * @email chichiooo@sina.com
     * @DateTime 2024-09-09
     * @param Request $request
     * @return void
     */
    public function getSpeaker(Request $request)
    {
        $where = $request->more([
            ['name', ''],
            ['status', '1'],
        ]);
        $query = $this->logic->search($where);
        $data = $this->logic->where($query)->field('id,name,name_en,title,email')->order('id', 'desc')->select();
        return $this->success($data);
    }

    /**
     * 获取国家
     *
     * @Author hechengwei
     * @email chichiooo@sina.com
     * @DateTime 2024-09-09
     * @param Request $request
     * @return void
     */
    public function getCountry(Request $request)
    {
        $where = $request->more([
            ['type', ''],
            ['status', '1'],
            ['parent_id', '']
        ]);

        $category = Category::where($where)->field('id,category_name_cn,category_name_en')->order('sort', 'desc')->select();
        return $this->success($category);
    }

    /**
     * 获取城市
     *
     * @Author hechengwei
     * @email chichiooo@sina.com
     * @DateTime 2024-09-09
     * @param Request $request
     * @return void
     */
    public function getCity(Request $request)
    {
        $where = $request->more([
            ['type', ''],
            ['status', '1'],
        ]);
        $country_id = $request->input('country_id');

        $category = Category::where($where)->where('parent_id', $country_id)->field('id,category_name_cn,category_name_en')->order('sort', 'desc')->select();
        return $this->success($category);
    }
}
